// pages/search/search.js

const app = getApp()
const api = require('../../utils/util')
Page({
    // 页面分享
    onShareAppMessage () {},
    // 分享朋友圈
    onShareTimeline () {},

    /**
     * 页面的初始数据
     */
    data: {
        imagesUrl: app.globalData.imagesUrl,
        siteUrl: app.globalData.siteUrl,
        // 搜素
        value: '',
        // 列表数据
        list1: [
            {
                url: app.globalData.imagesUrl + 'list_p1.jpg',
                title: '广告设计',
                price: '￥500~1500',
                companyLogoUrl: app.globalData.imagesUrl + 'list_lg1.png',
                companyTitle: '上海咖墨文化...',
                tag: '文化传媒'
            },
            {
                url: app.globalData.imagesUrl + 'list_p2.jpg',
                title: '软件设计',
                price: '￥500~1500',
                companyLogoUrl: app.globalData.imagesUrl + 'list_lg1.png',
                companyTitle: '上海咖墨文化...',
                tag: '数字科技'
            },
            {
                url: app.globalData.imagesUrl + 'list_p3.jpg',
                title: '房屋建设',
                price: '￥500~1500',
                companyLogoUrl: app.globalData.imagesUrl + 'list_lg1.png',
                companyTitle: '上海咖墨文化...',
                tag: '房产建设'
            },
            {
                url: app.globalData.imagesUrl + 'list_p4.jpg',
                title: '网站设计',
                price: '￥500~1500',
                companyLogoUrl: app.globalData.imagesUrl + 'list_lg1.png',
                companyTitle: '上海咖墨文化...',
                tag: '文化传媒'
            },
            {
                url: app.globalData.imagesUrl + 'list_p5.jpg',
                title: '网站设计',
                price: '￥500~1500',
                companyLogoUrl: app.globalData.imagesUrl + 'list_lg1.png',
                companyTitle: '上海咖墨文化...',
                tag: '文化传媒'
            },
            {
                url: app.globalData.imagesUrl + 'list_p6.jpg',
                title: '网站设计',
                price: '￥500~1500',
                companyLogoUrl: app.globalData.imagesUrl + 'list_lg1.png',
                companyTitle: '上海咖墨文化...',
                tag: '文化传媒'
            }
        ],
        list1Id: 1,
        // 切换置顶
        tabsList: [
            {
                title: '产品/服务'
            },
            {
                title: '企业'
            },
            {
                title: '会员'
            }
        ],
        tabsCurrent: 0,
        stickyScroll: false,
        // 分类弹窗
        typePopList: [
            {
                txt: '文化传媒'
            },
            {
                txt: '数字科技'
            },
            {
                txt: '法律咨询'
            },
            {
                txt: '房产建筑'
            },
            {
                txt: '物流贸易'
            },
            {
                txt: '专业咨询'
            },
            {
                txt: '医疗制药'
            },
            {
                txt: '机械制造'
            },
            {
                txt: '能源化工'
            },
            {
                txt: '物流贸易'
            },
            {
                txt: '消费零售'
            },
            {
                txt: '商务金融'
            },
            {
                txt: '土木工程'
            },
            {
                txt: '互联网通讯'
            }
        ],
        typePopShow: false,
        // 历史搜索
        hisSearchList: [
            {
                txt: '广告设计'
            },
            {
                txt: '软件设计'
            },
            {
                txt: '法律咨询'
            },
            {
                txt: '互联网通讯'
            }
        ],
        hisSearchCurrent: -1,
        // 热门推荐
        hotRecommendList: [
            {
                txt: '网站设计'
            },
            {
                txt: '广告设计'
            },
            {
                txt: '软件设计开发'
            },
            {
                txt: '房地产'
            },
            {
                txt: '零售批发'
            },
            {
                txt: '法律咨询'
            },
            {
                txt: '通讯'
            },
            {
                txt: '家具定制'
            }
        ],
        hotRecommendCurrent: -1,
        category: '',
        placeholder: '请输入产品服务名称'
    },
    onLoad() {
      let param = {
        str:'',
        category: ''
      }
      let that = this
        //搜索记录
        this.searchList(that)
    },
    //获取分类
    category(that){
    
      api.category_list().then(function(res){
        let typePopList = [];
      
        for(let i = 0; i< res.data.length; i++){
          typePopList.push({
            txt: res.data[i].title
          })
        }
         that.setData({
          typePopList
         })
      })
    },

    searchList(that){
      let ucode = app.globalData.userinfo.ucode
      let param = {
        ucode : ucode
      }

      api.search_list(param).then(function(res){
        console.log(param)
       let hisSearchList = [];
       let hotRecommendList = [];
       for(let i =0; i< res.data.hisSearchList.length; i++){
        hisSearchList.push({
          txt: res.data.hisSearchList[i].title
        })
       }
       for(let i =0; i< res.data.hotRecommendList.length; i++){
        hotRecommendList.push({
          txt: res.data.hotRecommendList[i].title
        })
       }
        that.setData({
          hotRecommendList: hotRecommendList,
          hisSearchList: hisSearchList
        })
      })
    },
    // 刷新新瀑布流
    
    update() {
        this.data.list1Id = 1
        // 重置瀑布流组件
        this.selectComponent('#waterfall').reset()
        let list1 = this.getMockData()
        this.setData({ list1 })
        wx.stopPullDownRefresh()
    },
    // 获取瀑布流数据
    getMockData() {
        let { list1Id } = this.data
        let list1 = []
        for (let i = 0; i < this.data.list1.length; i++) {
            list1.push({
                list1Id,
                imgUrl: app.globalData.siteUrl + this.data.list1[i].url,
                title: this.data.list1[i].title+'',
                price: this.data.list1[i].price+'',
                companyLogoUrl: app.globalData.siteUrl + this.data.list1[i].companyLogoUrl,
                companyTitle: this.data.list1[i].companyTitle+'',
                tage: this.data.list1[i].tag+'',
                id: this.data.list1[i].id
            })

            this.data.list1Id = ++list1Id
        }
        return list1
    },
    // 切换置顶
    onStickyScroll(e) {
        let stickyScroll = false
        if(e.detail.isFixed) {
            stickyScroll = !stickyScroll
        }
        this.setData({stickyScroll})
    },
    // 切换
    onTabsClick(e) {
        let tabsCurrent = e.target.dataset.index
        console.log(tabsCurrent)
        if(tabsCurrent == 0){
          this.setData({
            placeholder: '请输入产品/服务名称'
          })
        }else if(tabsCurrent == 1){
          this.setData({
            placeholder: '请输入公司名称、人名等关键词'
          })
        }else{
          this.setData({
            placeholder: '请输入会员名称'
          })
        }
        this.setData({tabsCurrent})
    },
    // 分类弹窗
    typeShowPopup() {
        this.setData({ typePopShow: true });
    },
    // 历史搜索
    onHisSearchClick(e) {
      
        let hisSearchCurrent = e.currentTarget.dataset.index
        this.setData({hisSearchCurrent})
        this.setData({hotRecommendCurrent: -1})
        this.setData({
          value:e.currentTarget.dataset.name
        })
        //this.onSearch()
    },
    // 热门推荐
    hotRecommendList(e) {
        let hotRecommendCurrent = e.currentTarget.dataset.index
        this.setData({hotRecommendCurrent})
        this.setData({hisSearchCurrent: -1})
        this.setData({
          value:e.currentTarget.dataset.name
        })
        //this.onSearch()
    },
    
    onChange(e){
      this.setData({
        value: e.detail
      })
    },
    onSearch(){
        let param = {
          ucode: app.globalData.userinfo.ucode,
          str : this.data.value
        }
        let that = this
        api.search_index(param).then(function(res){
           wx.redirectTo({
             url: '/pages/searchM/search?str=' + that.data.value + '&tabsCurrent=' + that.data.tabsCurrent,
           })
        })
    },
    del: function(){
      let param = {
        ucode: app.globalData.userinfo.ucode
      }
      let that = this
      api.search_del(param).then(function(){
        
        //搜索记录
        that.searchList(that)
      })
    }
})